import 'package:flutter/material.dart';
import 'package:helloworld/tabs.dart';
import 'package:dio/dio.dart';
import 'dart:ui';

class AddDebt extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AddDebt();
  }
}

class _AddDebt extends State<AddDebt> {
  var _name = TextEditingController();
  var _note = TextEditingController();
  var _debt = TextEditingController();
  Brightness brightnessValue;
  addDebt() async {
    Response response = await Dio()
        .post("http://123.207.114.127:1314/debt/addDebt", queryParameters: {
      'debtname': _name.text,
      'debtnote': _note.text,
      'debt': -double.parse(_debt.text),
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    brightnessValue = window.platformBrightness;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text('添加负债'),
          actions: <Widget>[
            InkWell(
              child: Padding(
                child: Text(
                  '保存',
                  style: TextStyle(color: Colors.white, fontSize: 20),
                ),
                padding: EdgeInsets.all(16),
              ),
              onTap: () {
                addDebt();
                Future.delayed(Duration(milliseconds: 50), () {
                  Navigator.of(context).pop();
                  Navigator.of(context).pushAndRemoveUntil(
                      new MaterialPageRoute(
                          builder: (context) => new Tabs(index: 3)),
                      (route) => route == null);
                });
              },
            )
          ],
        ),
        body: Padding(
          padding: EdgeInsets.all(20),
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                TextField(
                  autofocus: true,
                  keyboardAppearance: brightnessValue,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(), labelText: '名称'),
                  onChanged: (value) {
                    setState(() {
                      _name.text = value;
                    });
                  },
                ),
                SizedBox(
                  height: 10,
                ),
                TextField(
                  keyboardAppearance: brightnessValue,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(), labelText: '备注'),
                  onChanged: (value) {
                    setState(() {
                      _note.text = value;
                    });
                  },
                ),
                SizedBox(
                  height: 10,
                ),
                TextField(
                  keyboardAppearance: brightnessValue,
                  keyboardType: TextInputType.numberWithOptions(decimal: true),
                  decoration: InputDecoration(
                      border: OutlineInputBorder(), labelText: '金额'),
                  onChanged: (value) {
                    setState(() {
                      _debt.text = value;
                    });
                  },
                )
              ],
            ),
            scrollDirection: Axis.vertical,
          ),
        ));
  }
}
